package com.vibeplayer.app.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.vibeplayer.app.MainActivity;
import com.vibeplayer.app.R;
import com.vibeplayer.app.adapter.SongAdapter;
import com.vibeplayer.app.db.DatabaseHelper;
import com.vibeplayer.app.model.Playlist;
import com.vibeplayer.app.model.Song;
import com.vibeplayer.app.service.MusicPlayerService;
import com.vibeplayer.app.util.MediaScanner;

import java.util.ArrayList;
import java.util.List;

public class PlaylistDetailFragment extends Fragment implements SongAdapter.SongClickListener {

    private static final String ARG_PLAYLIST_ID = "playlist_id";

    private RecyclerView recyclerView;
    private SongAdapter adapter;
    private List<Song> songs = new ArrayList<>();
    private TextView txtPlaylistName;
    private TextView txtNoSongs;
    private ImageButton btnBack;
    private DatabaseHelper databaseHelper;
    private MediaScanner mediaScanner;
    private long playlistId;
    private Playlist playlist;

    public static PlaylistDetailFragment newInstance(long playlistId) {
        PlaylistDetailFragment fragment = new PlaylistDetailFragment();
        Bundle args = new Bundle();
        args.putLong(ARG_PLAYLIST_ID, playlistId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            playlistId = getArguments().getLong(ARG_PLAYLIST_ID);
        }
        databaseHelper = DatabaseHelper.getInstance(requireContext());
        mediaScanner = new MediaScanner(requireContext());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_playlist_detail, container, false);
        
        recyclerView = view.findViewById(R.id.recyclerViewPlaylistSongs);
        txtPlaylistName = view.findViewById(R.id.txtPlaylistName);
        txtNoSongs = view.findViewById(R.id.txtNoPlaylistSongs);
        btnBack = view.findViewById(R.id.btnBack);
        
        // 设置返回按钮点击事件
        btnBack.setOnClickListener(v -> requireActivity().onBackPressed());
        
        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SongAdapter(getContext(), songs, this);
        recyclerView.setAdapter(adapter);
        
        // 加载播放列表信息
        loadPlaylistInfo();
        
        // 加载播放列表歌曲
        loadPlaylistSongs();
        
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 刷新播放列表歌曲
        loadPlaylistSongs();
    }

    private void loadPlaylistInfo() {
        playlist = databaseHelper.getPlaylist(playlistId);
        if (playlist != null) {
            txtPlaylistName.setText(playlist.getName());
        }
    }

    private void loadPlaylistSongs() {
        songs.clear();
        
        // 获取播放列表中的歌曲ID列表
        List<Long> songIds = databaseHelper.getPlaylistSongIds(playlistId);
        
        // 根据ID获取歌曲信息
        if (!songIds.isEmpty()) {
            songs.addAll(mediaScanner.getSongsByIds(songIds));
        }
        
        if (songs.isEmpty()) {
            txtNoSongs.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            txtNoSongs.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onSongClick(int position) {
        // 通知MainActivity播放选中的歌曲
        if (getActivity() instanceof MainActivity) {
            MainActivity activity = (MainActivity) getActivity();
            if (activity.isServiceBound()) {
                MusicPlayerService musicService = activity.getMusicService();
                if (musicService != null) {
                    musicService.setSongs(songs);
                    activity.playSong(position);
                }
            }
        }
    }
} 